package com.example.user;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.example.common.network.IStartGameDetailActivity;
import com.example.user.databinding.UserMyGameRvItemBinding;
import com.example.user.viewmodel.MyGamePageViewModel;

import org.jetbrains.annotations.NotNull;


public class MyGameRAdapter extends RecyclerView.Adapter<MyGameRAdapter.MyGameRHolder>{
    private MyGamePageViewModel viewModel;
    public IStartGameDetailActivity iStartGameDetailActivity;//点击跳转接口

    public MyGameRAdapter(MyGamePageViewModel myGamePageViewModel){
        viewModel=myGamePageViewModel;
    }

    @Override
    public MyGameRHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        UserMyGameRvItemBinding binding= DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()),
                R.layout.user_my_game_rv_item,
                parent,
                false
        );
        MyGameRHolder holder=new MyGameRHolder(binding);
        return holder;
    }

    @Override
    public void onBindViewHolder( MyGameRAdapter.MyGameRHolder holder, int position) {
        //数据绑定
        holder.binding.setData(viewModel.getUserGames().get(position));
        //点击事件
        holder.binding.myGameRvIIconIv.setOnClickListener(v -> iStartGameDetailActivity.openById(holder.binding.getData().getGame().getId()));
    }

    @Override
    public int getItemCount() {
        if(viewModel.getUserGames()!=null){
            return viewModel.getUserGames().size();
        }else {
            return 0;
        }
    }

    /*****************************************************************************/
    class MyGameRHolder extends RecyclerView.ViewHolder{
        private UserMyGameRvItemBinding binding;

        public MyGameRHolder(UserMyGameRvItemBinding userMyGameRvItemBinding) {
            super(userMyGameRvItemBinding.getRoot());
            binding=userMyGameRvItemBinding;
        }
    }
}
